package i2.application.checklist.struts.actions;

import i2.application.checklist.entities.Application;
import i2.application.checklist.services.AdministrationService;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;


public class ApplicationAction extends ActionSupport  {

	private static final long serialVersionUID = 1L;
	private static final Logger logger = LoggerFactory.getLogger(ApplicationAction.class);

	private AdministrationService administrationService;

	private List<Application> enumApplication;

	private Application application;

	private int id;

	private boolean creation = false;
//	private Scrud<Application> scrud;
	
	////////////////////////////////////////////////////////////////////////
	
	public String input() throws Exception {
		return "input";
	}

	public String back() {
		return "back";
	}

//	public String search() {
//		this.setMangas(this.mangaService.search(scrud.getEntity()));
//		return "input";
//	}

//	public String create() {
//		scrud.setAction(Action.CREATION);
//		return "detail";
//	}
//
//	public String update() {
//		scrud.setAction(Action.UPDATE);
//		return "detail";
//	}	
	
	////////////////////////////////////////////////////////////////////////
	public String list() {
		return "list";
	}

	public String consulter() {
		if (id == 0) {
			logger.error("aucun identifiant présent en paramètre");
			return ERROR;
		}

		logger.debug("consultation de l'application d'id : {}",id);
		
		try {
			application = enumApplication.get(enumApplication.indexOf(new Application(id)));
		} catch (IndexOutOfBoundsException e) {
			logger.error("{} n'est un identifiant connu",id);
			return ERROR;
		}
		return "view";
	}

	public String creer() {
//		scrud.setAction(ScrudAction.CREATE);
		creation = true;
		return INPUT;
	}

	public String editer() {
		///////////////////////////////////////////////////////////////////////////
//		scrud.setAction(ScrudAction.UPDATE);
//		if (scrud.getEntity() != null && scrud.getEntity().getIdManga() != null) {
//			scrud.setEntity(mangaService.read(scrud.getEntity().getIdManga()));
//		} else {
//			scrud.setEntity(new Manga());
//		}
		///////////////////////////////////////////////////////////////////////////
		logger.debug("edition de l'application d'id : {}",application.getId());
		try {
			application = enumApplication.get(enumApplication.indexOf(application));
		} catch (IndexOutOfBoundsException e) {
			logger.error("{} n'est un identifiant connu",id);
			return ERROR;
		}

		return INPUT;
	}

	public String modifier() {
		administrationService.updateApplication(application);
		return "list";
	}

	public String enregistrer() {
		administrationService.createApplication(application);
		return "list";
	}

	public String supprimer() {
		try {
			application = enumApplication.get(enumApplication.indexOf(application));
		} catch (IndexOutOfBoundsException e) {
			logger.error("{} n'est un identifiant connu",id);
			return ERROR;
		}
		administrationService.deleteApplication(application);

		return "list";

	}

	public void setAdministrationService(AdministrationService administrationService) {
		this.administrationService = administrationService;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Application getApplication() {
		return application;
	}

	public void setApplication(Application application) {
		this.application = application;
	}

	public List<Application> getEnumApplication() {
		return enumApplication;
	}

	public void setEnumApplication(List<Application> enumApplication) {
		this.enumApplication = enumApplication;
	}
	
	public boolean isCreation() {
		return creation;
	}

}
